﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Reflection.Emit;
using yunbo.Model.BIP;
using yunbo.Repositorys.FPMRepositorys;
using yunbo.Repositorys.SSMRepositorys;

namespace yunbo.Controllers.BIPControllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class BD_CPSXController : ControllerBase
    {
        BD_CPSXRepository bD_CPSXRepository = new BD_CPSXRepository();
        private readonly IWebHostEnvironment webHostEnvironment;
        public BD_CPSXController(IWebHostEnvironment webHostEnvironment)
        {
            this.webHostEnvironment = webHostEnvironment;
        }
        //查询属性名称
        [HttpGet("GetBD_CPSX")]
        public IActionResult GetBD_CPSX(string name)
        {
            List<BD_CPSXDY> bD_CPSXDies = bD_CPSXRepository.GetBD_CPSX(name);
            return Ok(bD_CPSXDies);
        }
        //分组查询属性名称
        [HttpGet("GetBD_CPSXFZ")]
        public IActionResult GetBD_CPSXFZ()
        {
            List<BD_CPSXDY> bD_CPSXDies = bD_CPSXRepository.GetBD_CPSXFZ();
            return Ok(bD_CPSXDies);
        }
        [HttpPost("Upload")]
        public IActionResult Upload(IFormFileCollection file)
        {
            string name1 = "";
            foreach (var item in file)
            {
                //生成文件名
                string name = DateTime.Now.ToString("yyyyMMddHHmmssfff");
                //获取后缀名
                string ext = item.FileName.Substring(item.FileName.LastIndexOf('.'));
                //设置上传路径
                //string path = $"Uploader/{DateTime.Now.ToString("yyyy/MM/dd/")}" + name1;
                string path = $"wwwroot/images/" + name + ext;
                //Core生成文件的绝对路径
                var jpath = Path.Combine(webHostEnvironment.ContentRootPath, path);
                //创建上传文件对应的文件夹
                (new FileInfo(jpath)).Directory.Create();
                using (FileStream stream = new FileStream(jpath, FileMode.CreateNew))
                {
                    item.CopyTo(stream);//图片上传
                                        //TempData["name"] = name + ext;//保存图片的值
                }
                name1 = name + ext;

            }
            return Content(name1);
        }
        [HttpGet("Getcpsxname")]
        public IActionResult Getcpsxname(string code)
        {
            var quans = bD_CPSXRepository.Getcpsxname(code);
            return Ok(quans);
        }
        [HttpPost("PostBD_CPSX")]
        public IActionResult PostBD_CPSX(BD_CPSX bD_CPSX)
        {
            bool rs = bD_CPSXRepository.PostBD_CPSX(bD_CPSX) > 0;
            return Ok(rs);
        }
    }
}
